﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KALE_Scanning {
    public class Utility {

        public static string NewFileNameContainer = "";
        public static string NewChoiceValue = "";
        public static string CheckLastSlash(string value)
        {
            try {
                if (value.EndsWith("/"))
                    return value;
                else
                    return value + "/";
            } catch  {

                return value;
            }
        }
        public static string CheckPathSlash(string Value)
        {
            try {
                if (Value.EndsWith("\\"))
                    return Value;
                else
                    return Value + "\\";
            } catch  {

                return Value;
            }
        }

        public static AGC_WebServices.Library GetNullLib()
        {
            AGC_WebServices.Library nullLib = new AGC_WebServices.Library();
            nullLib.LibName = "Kütüphane Seçiniz";
            nullLib.SubSiteName = "/";
            return nullLib;
            
        }
        public static AGC_WebServices.ContentType GetNullContentType()
        {
            AGC_WebServices.ContentType nullCType = new AGC_WebServices.ContentType();
            nullCType.Name = "İçerik Türü Seçiniz";
            nullCType.ContentTypeID = "123456";
            return nullCType;
        }

        public static bool CheckControl(System.Windows.Forms.Control ctrl)
        {
            bool retValue = false;
            switch (ctrl.GetType().ToString()) {
                case "System.Windows.Forms.TextBox":
                    if (!string.IsNullOrEmpty((ctrl as System.Windows.Forms.TextBox).Text.Trim()))
                        retValue = true;
                    break;
                case "System.Windows.Forms.CheckedListBox":
                    if((ctrl as System.Windows.Forms.CheckedListBox).CheckedItems.Count > 0)
                        retValue = true;
                    break;
                case "System.Windows.Forms.RichTextBox":
                    if (!string.IsNullOrEmpty((ctrl as System.Windows.Forms.RichTextBox).Text.Trim()))
                        retValue = true;
                    break;
                case "System.Windows.Forms.RadioButton":
                    if((ctrl as System.Windows.Forms.RadioButton).Checked == true)
                        retValue = true;
                    break;
                case "System.Windows.Forms.ComboBox":
                    if ((ctrl as System.Windows.Forms.ComboBox).SelectedIndex >= 0)
                        retValue = true;
                    break;
                default:
                    retValue = false;
                    break;
            }
            return retValue;
        }

        public static string RequiredLabelControl(AGC_WebServices.Control fieldForControls)
        {
            string requiredLabelText;
            if(fieldForControls.IsRequired == true)
            {
                requiredLabelText = fieldForControls.Isim + ": *";
            }
            else
            {
                requiredLabelText = fieldForControls.Isim + ":";
            }
            return requiredLabelText;
        }

        public static bool UniqueDataValidate(System.Windows.Forms.Control ctrl, ControlType cType, AGC_WebServices.Control fieldForControl, string siteUrl, string libraryName)
            {
            string a = fieldForControl.StaticName;
            AGC_WebServices.Service ws = new AGC_WebServices.Service();
            //ws.UseDefaultCredentials = true;
            ws.Credentials = UserProperty.UserProperties.Credential;
            bool retVal = true;
            if(ctrl.GetType().Name == "TextBox")
            {
                System.Windows.Forms.TextBox txt = (ctrl as System.Windows.Forms.TextBox);
                string TrimedText = txt.Text.Trim();
                switch (cType)
	            {
		            case ControlType.Numeric:
                        if(ws.ValidateUniqueData(siteUrl,libraryName,fieldForControl, TrimedText) == false)
                        {
                        System.Windows.Forms.MessageBox.Show("Bu veri sistemde kayıtlı");
                        txt.SelectAll();
                        retVal = false;
                        }
                        break;
                    case ControlType.Text:
                        if (ws.ValidateUniqueData(siteUrl, libraryName, fieldForControl, TrimedText) == false) 
                        {
                        System.Windows.Forms.MessageBox.Show("Bu veri sistemde kayıtlı");
                        txt.SelectAll();
                        retVal = false;
                        }
                        break;
                    default:
                        retVal = false;
                        break;
	            }
            }
            if (ctrl.GetType().Name == "ComboBox") 
            {
                System.Windows.Forms.ComboBox cmbox = (ctrl as System.Windows.Forms.ComboBox);
                switch (cType) 
                {
                    case ControlType.Combobox:
                        if (cmbox.SelectedItem == null)
                        {
                            System.Windows.Forms.MessageBox.Show("Bu veri sistemde kayıtlı");
                            cmbox.SelectAll();
                            retVal = false;
                        }
                        break;
                    default:
                        retVal = false;
                        break;
                }
            }

             if (ctrl.GetType().Name == "DateTimePicker")
             {

                System.Windows.Forms.DateTimePicker dateTime = (ctrl as System.Windows.Forms.DateTimePicker);
                switch (cType)
                {
                 case ControlType.DateTime:
                 if (dateTime.Text == null) 
                   {
                            System.Windows.Forms.MessageBox.Show("Bu veri sistemde kayıtlı");
                            //dateTime.SelectAll();
                            retVal = false;
                   }
                   break;
                 default:
                   retVal = false;
                   break;
                }}

            return retVal;
        }

        #region XML Sifreleme method ları
        public static string Sifrele(string clear)
        {
            //Xml Dosyasındaki bilgilerin gizliliği için şifreleme sağlar

            int lenght = clear.Length;
            char[] a = clear.ToCharArray();
            for (int i = 0; i < lenght; i++)
            {
                a[i] = Convert.ToChar(Convert.ToInt32(a[i]) + 13);
            }
            clear = "";
            for (int i = 0; i < lenght; i++)
            {
                clear += a[i].ToString();
            }
            return clear;
        }

        public static string Coz(string bad)
        {
            //Xml Dosyasındaki bilgilerin program tarafından anlaşılabilir olmasını sağlar

            int lenghtt = bad.Length;
            char[] a = bad.ToCharArray();
            for (int i = 0; i < lenghtt; i++)
            {
                a[i] = Convert.ToChar(Convert.ToInt32(a[i]) - 13);
            }
            bad = "";
            for (int i = 0; i < lenghtt; i++)
            {
                bad += a[i].ToString();
            }
            return bad;
        }
        #endregion
    }

    public enum UploadType
    {
        Browse = 1,
        Scanner = 2
    }

    public enum ControlType
    {
        Numeric = 1,
        Combobox = 2,
        Text = 3,
        DateTime = 4
    }
}
